angular.module('media.controller', [])
    .controller('mediaController', ['$charitySocietyService', '$stateParams', '$sce', '$scope',
        function ($charitySocietyService, $stateParams, $sce, $scope) {
        var id = $stateParams.id;

        $scope.items = [{
            name:'媒体报道',
            url:'/#/mediaList/10',
            type:0,
            showChildren:true,
            children:[{
                name:'机构动态',
                type:10,
                url:'/#/mediaList/10'
            },{
                name:'项目动态',
                url:'/#/mediaList/11',
                type:11
            },{
                name:'行业动态',
                url:'/#/mediaList/12',
                type:12
            }]
        },{
            name:'视频',
            url:'/#/mediaList/9',
            type:9,
            showChildren:false
        }];

            $scope.activeHead = function (item) {
                if(item.type == $scope.selectedType){
                    return true;
                }
                else{
                    if(item.children){
                        for(var i =0; i< item.children.length;i++){
                            if(item.children[i].type ==$scope.selectedType){
                                return  true;
                            }
                        }
                    }
                    return false;
                }
            };

        $charitySocietyService.getArticleDetail({articleId: id}, function (data) {
            $scope.articleTitle = data.data[0] ? data.data[0].title : '';
            $scope.contentHtml = $sce.trustAsHtml(data.data[0] ? data.data[0].content : '');
            $scope.updateAt = data.data[0].update_at;
            $scope.selectedType = data.data[0].article_type;
        }, function (err) {
            console.error(err);
        });
    }])
    .controller('mediaListController', ['$scope', '$charitySocietyService','$state','$stateParams', 
    function ($scope, $charitySocietyService,$state,$stateParams) {
    //    console.log("mediaListController Type: "+ $stateParams.articleType);

        $scope.items = [{
            name:'媒体报道',
            url:'/#/mediaList/10',
            type:0,
            showChildren:true,
            children:[{
                name:'机构动态',
                type:10,
                url:'/#/mediaList/10'
            },{
                name:'项目动态',
                url:'/#/mediaList/11',
                type:11
            },{
                name:'行业动态',
                url:'/#/mediaList/12',
                type:12
            }]
        },{
            name:'视频',
            url:'/#/mediaList/9',
            type:9,
            showChildren:false
        }];

        $scope.selectedType = $stateParams.articleType;

        $scope.flatFlag = true;

        $scope.selectedYear = 2016;
        $scope.selectedTab = function (type) {
            if ($scope.selectedType == type) {
                return;
            }     
            $scope.selectedType = type;
            $scope.queryArticles($scope.selectedType);
        }

        $scope.toggleTab = function () {
            $scope.flatFlag = !$scope.flatFlag;
            console.log($scope.flatFlag);
            return;
        }

        $scope.selectYear = function(year){
            if($scope.selectedYear == year){
                return;
            }
            $scope.selectedYear = year;
            $scope.queryArticles();
        }


        $scope.activeHead = function (item) {
            if(item.type == $scope.selectedType){
                return true;
            }
            else{
                if(item.children){
                    for(var i =0; i< item.children.length;i++){
                        if(item.children[i].type ==$scope.selectedType){
                            return  true;
                        }
                    }
                }
                return false;
            }
        };

        $scope.articles = [];
        $scope.queryArticles = function () {
            $charitySocietyService.getArticlesByYear({
                pageNo: 0,
                articleType: $scope.selectedType,
                numPerPage: 10,
                year:$scope.selectedYear
            }, function (data) {
                $scope.articles = [];
                if (null != data && data.data && data.data.length > 0) {
                    $scope.articles = data.data;
                }
                init();
            }, function (err) {
                console.error(err);
            });
        };

        $scope.getStyle = function(typeArray){
        
            for(var i = 0;i<typeArray.length; ++ i){
                if( $scope.selectedType == typeArray[i] ){
                    return {
                        "background":"#95B5E6"
                    }
                }
            }
           
            return {
                "background":"#777777"
            }
            
        }

        $scope.getLiStyle = function(typeArray){
            for(var i = 0;i<typeArray.length; ++ i){
                if( $scope.selectedType == typeArray[i] ){
                    return {
                        "color":"#0172e3"
                    }
                }
            }
           
            return {
                "color":"#777777"
            }
        }
        $scope.goto = function(type){
            $state.go('mediaList', {articleType: type});
        }

        


        $scope.row = '';

        $scope.select = function(page) {
            var end, start;
            start = (page - 1) * $scope.numPerPage;
            end = start + $scope.numPerPage;
            return $scope.currentPageArticles = $scope.articles.slice(start, end);
        };

        $scope.onFilterChange = function() {
            $scope.select(1);
            $scope.currentPage = 1;
            return $scope.row = '';
        };

        $scope.onNumPerPageChange = function() {
            $scope.select(1);
            return $scope.currentPage = 1;
        };

        $scope.onOrderChange = function() {
            $scope.select(1);
            return $scope.currentPage = 1;
        };

        $scope.numPerPageOpt = [3, 5, 10, 20];
        $scope.numPerPage = $scope.numPerPageOpt[1];
        $scope.currentPage = 1;

        $scope.currentPageArticles = [];

        init = function() {
            return $scope.select($scope.currentPage);
        };

    $scope.queryArticles($scope.selectedType);
}])
